home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 4 / Example 4.3 / heightMap3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-12-04  |  3.5 KB  |  153 lines

  1. #include "heightMap3.h"
  2. #include "debug.h"
  3.  
  4. const DWORD PARTICLE::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
  5. DWORD FtoDword(float f){return *((DWORD*)&f);}
  6.  
  7. HEIGHTMAP3::HEIGHTMAP3(IDirect3DDevice9* Dev)
  8. {
  9.     try
  10.     {
  11.         Device = Dev;
  12.  
  13.         //Reset the heightMap to 0
  14.         maxHeight = 20.0f;
  15.         memset(&heightMap, 0, sizeof(float)*SIZE_X*SIZE_Y);
  16.  
  17.         //Set particle vertex buffer to NULL
  18.         vb = NULL;
  19.  
  20.         selRect.top = selRect.left = SIZE_X / 2 - 5;
  21.         selRect.bottom = selRect.right = SIZE_X / 2 + 5;
  22.     }
  23.     catch(...)
  24.     {
  25.         debug.Print("Error in HEIGHTMAP3::HEIGHTMAP()");
  26.     }
  27. }
  28.  
  29. HEIGHTMAP3::~HEIGHTMAP3()
  30. {
  31.     if(vb != NULL)vb->Release();
  32. }
  33.  
  34. void HEIGHTMAP3::RaiseTerrain(RECT r, float f)
  35. {
  36.     for(int y=r.top;y<=r.bottom;y++)
  37.         for(int x=r.left;x<=r.right;x++)
  38.         {
  39.             heightMap[x][y] += f;
  40.             if(heightMap[x][y] < -maxHeight)heightMap[x][y] = -maxHeight;
  41.             if(heightMap[x][y] > maxHeight)heightMap[x][y] = maxHeight;
  42.         }
  43.  
  44.     CreateParticles();
  45. }
  46.  
  47. void HEIGHTMAP3::SmoothTerrain()
  48. {
  49.     for(int y=0;y<SIZE_Y;y++)
  50.         for(int x=0;x<SIZE_X;x++)
  51.         {
  52.             float totalHeight = 0.0f;
  53.             int noNodes = 0;
  54.  
  55.             for(int y1=y-1;y1<=y+1;y1++)
  56.                 for(int x1=x-1;x1<=x+1;x1++)
  57.                     if(x1 >= 0 && x1 < SIZE_X && y1 >= 0 && y1 < SIZE_Y)
  58.                     {
  59.                         totalHeight += heightMap[x1][y1];
  60.                         noNodes++;
  61.                     }
  62.  
  63.             heightMap[x][y] = totalHeight / (float)noNodes;
  64.         }
  65.  
  66.     CreateParticles();
  67.  
  68.     Sleep(500);
  69. }
  70.  
  71. void HEIGHTMAP3::MoveRect(int dir)
  72. {
  73.     if(dir == LEFT)    {selRect.left--;selRect.right--;}
  74.     if(dir == RIGHT){selRect.left++;selRect.right++;}
  75.     if(dir == UP)    {selRect.top--;selRect.bottom--;}
  76.     if(dir == DOWN)    {selRect.top++;selRect.bottom++;}
  77.  
  78.     Sleep(100);
  79.     CreateParticles();
  80. }
  81.  
  82. HRESULT HEIGHTMAP3::CreateParticles()
  83. {
  84.     try
  85.     {
  86.         if(vb != NULL)
  87.         {
  88.             vb->Release();
  89.             vb = NULL;
  90.         }
  91.  
  92.         if(FAILED(Device->CreateVertexBuffer(SIZE_X * SIZE_Y * sizeof(PARTICLE), D3DUSAGE_DYNAMIC | D3DUSAGE_POINTS | D3DUSAGE_WRITEONLY, PARTICLE::FVF, D3DPOOL_DEFAULT, &vb, 0)))
  93.             debug.Print("Failed to create particle vertex buffer");
  94.  
  95.         PARTICLE *v = NULL;
  96.         vb->Lock(0, 0, (void**)&v, D3DLOCK_DISCARD);
  97.  
  98.         for(int y=0;y<SIZE_Y;y++)
  99.             for(int x=0;x<SIZE_X;x++)
  100.             {
  101.                 float prc = heightMap[x][y] / maxHeight;
  102.                 if(prc < 0.0f)prc = -prc;
  103.                 int red =  255 * prc;
  104.                 int green = 255 * (1.0f - prc);
  105.  
  106.                 if(x >= selRect.left && x <= selRect.right && y >= selRect.top && y <= selRect.bottom)
  107.                     v->color = D3DCOLOR_ARGB(255, 0, 0, 255);
  108.                 else v->color = D3DCOLOR_ARGB(255, red, green, 0);
  109.  
  110.                 v->position = D3DXVECTOR3(x, heightMap[x][y], -y);
  111.                 v++;
  112.             }
  113.         
  114.         vb->Unlock();
  115.  
  116.     }
  117.     catch(...)
  118.     {
  119.         debug.Print("Error in HEIGHTMAP3::CreateParticles()");
  120.         return E_FAIL;
  121.     }
  122.  
  123.     return S_OK;
  124. }
  125.  
  126. void HEIGHTMAP3::Render()
  127. {
  128.     try
  129.     {
  130.         if(vb != NULL)
  131.         {
  132.             Device->SetRenderState(D3DRS_LIGHTING, false);
  133.             Device->SetRenderState(D3DRS_POINTSPRITEENABLE, true);
  134.             Device->SetRenderState(D3DRS_POINTSCALEENABLE, true);
  135.  
  136.             Device->SetRenderState(D3DRS_POINTSIZE, FtoDword(0.7f));
  137.             Device->SetRenderState(D3DRS_POINTSIZE_MIN, FtoDword(0.0f));
  138.             Device->SetRenderState(D3DRS_POINTSCALE_A, 0);
  139.             Device->SetRenderState(D3DRS_POINTSCALE_B, FtoDword(0.0f));
  140.             Device->SetRenderState(D3DRS_POINTSCALE_C, FtoDword(1.0f));
  141.             Device->SetRenderState(D3DRS_ZWRITEENABLE, false);
  142.  
  143.             Device->SetTexture(0, NULL);
  144.             Device->SetFVF(PARTICLE::FVF);
  145.             Device->SetStreamSource(0, vb, 0, sizeof(PARTICLE));
  146.             Device->DrawPrimitive(D3DPT_POINTLIST, 0, SIZE_X * SIZE_Y);
  147.         }
  148.     }
  149.     catch(...)
  150.     {
  151.         debug.Print("Error in HEIGHTMAP3::Render()");
  152.     }
  153. }